﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaPortal.Database;

namespace MyEmulators2
{
    class DB_Updater
    {
        public DB_Updater()
        {
            loadUpdates();
        }

        List<Update> updates = null;

        void loadUpdates()
        {
            updates = new List<Update>();

            updates.Add(new Update(0.2, new Update.UpdateDelegate(delegate() 
                { 
                    DB.Instance.Execute("ALTER TABLE {0} ADD COLUMN escapetoexit char(5)", Emulator.TABLE_NAME);
                    DB.Instance.Execute("UPDATE {0} SET escapetoexit='False'", Emulator.TABLE_NAME);

                    DB.Instance.Execute("ALTER TABLE {0} ADD COLUMN escapetoexit char(5)", EmulatorProfile.TABLE_NAME);
                    DB.Instance.Execute("UPDATE {0} SET escapetoexit='False'", EmulatorProfile.TABLE_NAME);

                    DB.Instance.Execute("ALTER TABLE {0} ADD COLUMN hash varchar(100)", Game.TABLE_NAME);
                }
                )));
            //add Update's here
        }

        public void Update()
        {
            double currentDBVersion = DB.Instance.CurrentDBVersion;
            foreach (Update update in updates)
            {
                if (currentDBVersion < update.MinRequiredVersion)
                    update.UpdateMethod();
            }

            DB.Instance.UpdateDBVersion();
        }
    }

    class Update
    {
        public Update(double minRequiredVersion, UpdateDelegate updateMethod)
        {
            MinRequiredVersion = minRequiredVersion;
            UpdateMethod = updateMethod;
        }

        public delegate void UpdateDelegate();

        public double MinRequiredVersion
        {
            get;
            set;
        }

        public UpdateDelegate UpdateMethod
        {
            get;
            set;
        }
    }

}
